package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result rolelist(@RequestParam(name = "name", required = false) String name,
                           @RequestParam(name = "label", required = false) String label,
                           @RequestParam(name = "page",defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize) {
        PageResult<Role> pageResult = roleService.page(name, label, pageNum, pageSize);
        log.info("分页查询角色,参数:角色名称={},角色标签={},当前页码={},每页大小={}", name, label, pageNum, pageSize);
        return Result.success(pageResult);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除角色：{}",id);
        roleService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Role role) {
        log.info("添加角色：{}",role);
        roleService.add(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询角色：{}",id);
        Role role = roleService.findById(id);
        return Result.success(role);
    }
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("更新角色：{}",role);
        roleService.update(role);
        return Result.success();
    }
    @GetMapping("/list")
    public Result rolelist() {
        log.info("查询角色列表");
        List<Role> rolelist = roleService.rolelist();
        return Result.success(rolelist);
    }
}